package cn.dubito.miaosha.dao;

import cn.dubito.miaosha.domain.SeckillGoods;
import cn.dubito.miaosha.vo.GoodsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author liliang
 * @description:
 * @date 2018/3/31.
 */
@Mapper
public interface GoodsDao {

    /**
     * 获取商品列表
     *
     * @return
     */
    @Select("select g.*,sg.stock_count,sg.start_date,sg.end_date,sg.seckill_price from seckill_goods sg left join goods g on sg.goods_id = g.id")
    List<GoodsVO> getGoodsList();

    /**
     * 获取商品信息详情
     *
     * @param goodsId
     * @return
     */
    @Select("select g.*,sg.stock_count,sg.start_date,sg.end_date,sg.seckill_price from seckill_goods sg left join goods g on sg.goods_id = g.id where g.id=#{goodsId}")
    GoodsVO getGoodsByGoodsId(@Param("goodsId") long goodsId);

    /**
     * 更新库存信息
     *
     * @param g
     * @return
     */
    @Update("update seckill_goods set stock_count =  stock_count - 1 where goods_id = #{goodsId} and stock_count > 0")
    int reduceStock(SeckillGoods g);
}
